﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using Jungle.Code.Utils;
using Jungle.Prodamo.Core;
using Jungle.Prodamo.PriceManager;

namespace Jungle.Prodamo.User.WebControls
{
    public class PriceList : WebControl
    {
        private int getBy; // 0 - все, 1 - по категории, 2 - по ID, 3 - по модели, 4 - по производителю, 5 - по пользователю
        private int id;
        private int fk;

        public bool c = true;

        public int GetBy { get { return getBy; } set { getBy = value; } }
        public int iId { get { return id; } set { id = value; } }
        public int Fk { get { return fk; } set { fk = value; } }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (getBy != 5)
            {
                writer.AddAttribute("class", "advm_table");
                writer.RenderBeginTag(HtmlTextWriterTag.Table);
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute("class", "advm_td");
                writer.AddAttribute("style", "border-left: 1px solid #F1F1F1;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("Описание");
                writer.RenderEndTag();
                writer.AddAttribute("class", "advm_td");
                writer.AddAttribute("style", "border-left: 1px solid #F1F1F1;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("Цена розн. грн.");
                writer.RenderEndTag();
                writer.AddAttribute("class", "advm_td");
                writer.AddAttribute("style", "border-left: 1px solid #F1F1F1;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("Цена розн. у.е");
                writer.RenderEndTag();
                writer.AddAttribute("class", "advm_td");
                writer.AddAttribute("style", "border-left: 1px solid #F1F1F1;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("Гарантия");
                writer.RenderEndTag();
                writer.AddAttribute("class", "advm_td");
                writer.AddAttribute("style", "border-left: 1px solid #F1F1F1;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("Наличие");
                writer.RenderEndTag();
                writer.AddAttribute("class", "advm_td");
                writer.AddAttribute("style", "border-left: 1px solid #F1F1F1;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("Продавец");
                writer.RenderEndTag();
                writer.RenderEndTag();
            }
            switch (getBy)
            {
                case 1: GetByCtageory(writer); break;
                case 2:
                    {
                        Price pr = new Price();
                        pr.PriceId = id;
                        pr.GetById();
                        PriceItem(writer, pr, true);
                        break;
                    }
                case 3: GetByModel(writer); break;
                case 4: GetByProducer(writer); break;
                case 5: GetByUser(writer); break;
            }
            if (getBy != 5)
            {
                writer.RenderEndTag();
            }
            base.RenderContents(writer);
        }
        protected HtmlTextWriter GetByCtageory(HtmlTextWriter writer)
        {
            Price prs = new Price();
            prs.CategoryId = id;
            prs.GetByCategory();

            foreach (Price pr in prs.PriceList)
            {
                if (c) PriceItem(writer, pr, c);
                else { PriceItem(writer, pr, c); c = false; }
            }
            return writer;
        }
        protected HtmlTextWriter GetByModel(HtmlTextWriter writer)
        {
            Price prs = new Price();
            prs.ModelId = id;
            prs.GetByModel();

            foreach (Price pr in prs.PriceList)
            {
                if (c) PriceItem(writer, pr, c);
                else { PriceItem(writer, pr, c); c = false; }
            }
            return writer;
        }
        protected HtmlTextWriter GetByProducer(HtmlTextWriter writer)
        {
            Price prs = new Price();
            prs.ProducerId = id;
            prs.GetByProducer();

            foreach (Price pr in prs.PriceList)
            {
                if (pr.CategoryId == fk)
                {
                    if (c) PriceItem(writer, pr, c);
                    else { PriceItem(writer, pr, c); c = false; }
                }
            }
            return writer;
        }
        protected HtmlTextWriter GetByUser(HtmlTextWriter writer)
        {
            Price prs = new Price();
            prs.UserId = id;
            prs.GetByUser();

            writer.AddAttribute("class", "advm_table");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute("class", "advm_td");
            writer.AddAttribute("colspan", "2");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Прайс-листы");
            writer.RenderEndTag();
            writer.RenderEndTag();
            foreach (Category c in prs.Cats)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);

                writer.AddAttribute(HtmlTextWriterAttribute.Href, UrlHelper.GetSiteUrl() + "/ru/price.aspx?Id=" + c.CategoryId.ToString());
                writer.AddAttribute("style", "font-size: 12px; text-decoration: none;");
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(c.Name);
                writer.RenderEndTag();

                writer.RenderEndTag();
                writer.RenderEndTag();                
            }
            writer.RenderEndTag();
            return writer;
        }
        protected HtmlTextWriter PriceItem(HtmlTextWriter writer, Price pr, bool c)
        {
            if (!c) writer.AddAttribute("style", "background-color: #F1F1F1;");

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute("border-left", "1px solid #a6b7c8;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            Producer prod = new Producer();
            prod.ProducerId = pr.ProducerId;
            prod.GetById();
            writer.Write(pr.Description + "<br/><strong>Производитель:</strong> " + prod.Title);
            writer.RenderEndTag();
            writer.AddAttribute("style", "border-left: 1px solid #a6b7c8;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(pr.PriceGrnRozn);
            writer.RenderEndTag();
            writer.AddAttribute("style", "border-left: 1px solid #a6b7c8;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(pr.PriceUeRozn);
            writer.RenderEndTag();
            writer.AddAttribute("style", "border-left: 1px solid #a6b7c8;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(pr.Guarant + "мес.");
            writer.RenderEndTag();
            writer.AddAttribute("style", "border-left: 1px solid #a6b7c8;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            switch (pr.Have)
            {
                case 0: writer.Write("Есть(" + pr.PubDate.ToString().Substring(0, 10) + ")"); break;
                case 1: writer.Write("Нет"); break;
                case 2: writer.Write("Заказ"); break;
            }
            writer.RenderEndTag();
            writer.AddAttribute("style", "border-left: 1px solid #a6b7c8; text-align: center;");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
                UserManager um = new UserManager();
                um.UserId = pr.UserId;
                um.GetById();
                if (um.UsertType) writer.Write(um.UserLink());
                else writer.Write(um.CompanyLink());
                writer.Write("<br/>");
                if (um.Vip)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + "/Modules/PriceManager/img/vip.gif");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                }
            writer.RenderEndTag();

            writer.RenderEndTag();
            return writer;
        }
    }
}
